<?php
/**
 * This file is part of the Yacms (http://www.yacms.org)
 * Copyright (c) 2019 gylr0505 (http://www.yacms.org)
 *
 * Author: 鬼一浪人 <gylr0505@163.com>
 *
 * DateTime: 19-10-24 下午8:40
 * Description:  模板消息消费队列管理对象
 */
 
declare(strict_types = 1);

namespace App\Template\Queue;

use App\Template\TemplateProvider;

class Consumer extends Manager
{
    /**
     * 批量执行模板发送
     */
    private function batchSend()
    {
        $provider = new TemplateProvider();
        $control = ConsumerIntervalControl::getInstance();

        foreach ($this->objects as $params) {
            $control->load($params['appId'] ?? '', 600);
            //发现有失败，便不再执行
            if (!$control->isNext($params['appId'] ?? '')){
                continue;
            }

            $bool = $provider->generatorTemplate($params['id'],$params['type']);
            if (!$bool){
                continue;
            }
            $provider->getTemplate()->send($params);
        }
        $provider = null;
        $control = null;
        $this->objects  = null;
    }

    /**
     * @param callable|null $call
     */
    public function __invoke(callable $call = null)
    {
        $this->batchSend();
        $call();
    }

}